package com.bawei.admin.service.impl;

import com.bawei.admin.controller.Going;
import com.bawei.admin.feign.DoctorFeignService;
import com.bawei.admin.util.CheckIdCardUtil;
import com.bawei.admin.vo.CheckDoctorVo;
import com.bawei.admin.vo.CheckQueryVo;
import com.bawei.admin.vo.ReturnCheckIdVo;
import com.bawei.common.utils.R;
import com.bawei.common.vo.DoctorVo;
import com.bawei.common.vo.GradeMarkVo;
import com.bawei.common.vo.HospitalDoctorVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bawei.common.utils.PageUtils;
import com.bawei.common.utils.Query;

import com.bawei.admin.dao.AdminDao;
import com.bawei.admin.entity.AdminEntity;
import com.bawei.admin.service.AdminService;


@Service("adminService")
public class AdminServiceImpl extends ServiceImpl<AdminDao, AdminEntity> implements AdminService {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private Going going;
    @Autowired
    private DoctorFeignService doctorFeignService;

    @Autowired
    private CheckIdCardUtil checkIdCardUtil;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AdminEntity> page = this.page(
                new Query<AdminEntity>().getPage(params),
                new QueryWrapper<AdminEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public AdminEntity getAdminByAdminName(String adminName) {
        return baseMapper.getAdminByAdminName(adminName);
    }

    @Override
    public R listDoctorStatus(Map<String, Object> params) {

        IPage<AdminEntity> page = this.page(
                new Query<AdminEntity>().getPage(params),
                new QueryWrapper<AdminEntity>()
        );

        PageUtils pageUtils = new PageUtils(page);

        return R.ok().put("page", pageUtils);
    }

    /**
     * 展示所有的标题状态的医生
     * @param checkQueryVo
     * @return
     */
    @Override
    public PageUtils checkedDoctorList(CheckQueryVo checkQueryVo) {
        List<GradeMarkVo> redisGeaedMark = going.getRedisGeaedMark();

        List<HospitalDoctorVo> hospitalDoctorVos = going.getDoctorRedis();
        Integer status = checkQueryVo.getStatus();
        AtomicInteger add = new AtomicInteger();

        List<HospitalDoctorVo> collect = hospitalDoctorVos.stream().filter(it -> (it.getDoctorApprovalState().equals(status))).map(item -> {
            HospitalDoctorVo hospitalDoctorVo = new HospitalDoctorVo();
            if (redisGeaedMark != null) {
                for (GradeMarkVo gradeMarkVo : redisGeaedMark) {
                    if (gradeMarkVo.getId().equals(item.getAffiliatedHospitalId())) {
                        item.setHospitalName(gradeMarkVo.getTypeName());
                    }
                    //DepartmentName
                    //workName
                    if (gradeMarkVo.getId().equals(item.getAffiliatedDepartmentId())) {
                        item.setDepartmentName(gradeMarkVo.getTypeName());
                    }
                    if (gradeMarkVo.getId().equals(item.getDoctorGradeId())) {
                        item.setWorkName(gradeMarkVo.getTypeName());
                    }
                }
            }
            add.getAndIncrement();
            return item;
        }).collect(Collectors.toList());


        return new PageUtils(collect, collect.size(), Integer.valueOf(checkQueryVo.getLimit()), Integer.valueOf(checkQueryVo.getPage()));
    }


    @Override
    public R listDoctorMsgByDoctorId(Integer doctorId) {
        List<HospitalDoctorVo> hospitalDoctorVos = going.getDoctorRedis();
        List<GradeMarkVo> redisGeaedMark = going.getRedisGeaedMark();

        for (HospitalDoctorVo hospitalDoctorVo : hospitalDoctorVos) {
            Integer doctorId1 = hospitalDoctorVo.getDoctorId();

            System.out.println("mymy" + hospitalDoctorVo.getDoctorId() + "     " + doctorId);
            if (hospitalDoctorVo.getDoctorId().equals(doctorId)) {
                for (GradeMarkVo gradeMarkVo : redisGeaedMark) {
                    if (gradeMarkVo.getId().equals(hospitalDoctorVo.getAffiliatedHospitalId())) {
                        hospitalDoctorVo.setHospitalName(gradeMarkVo.getTypeName());
                    }
                    //DepartmentName
                    //workName
                    if (gradeMarkVo.getId().equals(hospitalDoctorVo.getAffiliatedDepartmentId())) {
                        hospitalDoctorVo.setDepartmentName(gradeMarkVo.getTypeName());
                    }
                    if (gradeMarkVo.getId().equals(hospitalDoctorVo.getDoctorGradeId())) {
                        hospitalDoctorVo.setWorkName(gradeMarkVo.getTypeName());
                    }
                }
                System.out.println(hospitalDoctorVo + "///////////////////////");
                String idCardNumber = hospitalDoctorVo.getIdCardNumber();
                String doctorName = hospitalDoctorVo.getDoctorName();

                ReturnCheckIdVo returnCheckIdVo = checkIdCardUtil.checkedidCard(idCardNumber, doctorName);
                System.out.println("returnCheckIdVo      returnCheckIdVo   "+returnCheckIdVo);
                if (returnCheckIdVo != null && returnCheckIdVo.getResult().getIsok()) {
                    return R.ok().put("hospitalDoctor", hospitalDoctorVo).put("check", true);
                }
                return R.ok().put("hospitalDoctor", hospitalDoctorVo).put("check", false);
            }
        }
        return null;

    }

    @Override
    public R checkDoctorStatus(CheckDoctorVo checkDoctorVo) {
        System.out.println(checkDoctorVo+"CheckDoctorVo");
        String s = doctorFeignService.updateStatus(checkDoctorVo.getDoctorId(), checkDoctorVo.getStatus());
        if ("1".equals(s)){
            going.addDoctorRedis();
            return R.ok();
        }else {
            return R.error().put("msg","审核失败");
        }
    }

    @Override
    public void addDoctor(DoctorVo doctorVo) {
        //生成规则为随机4位数字
        String uid = "";

        int i =(int)(Math.random()*9000+1000);
        String random = Integer.toString(i);
        uid=random;

        doctorVo.setDoctorNumber(uid);  //随时四位数做用户ID
        doctorVo.setDoctorApprovalState(0); //默认审核未通过 0
        doctorVo.setVersion(0);  //版本默认0
        doctorVo.setCaeateTime(new Date()); //创建时间为当前时间
        doctorVo.setDoctorEnquiryState(0);//默认问诊关闭状态
        doctorVo.setQualificationCertificateUpState(1);//默认资格证书已上传
        doctorVo.setPractisingCertificateUpState(1);//默认执业证书已上传
        doctorVo.setIdCardUpState(1);//默认身份证已上传
        doctorVo.setSumMoney(BigDecimal.valueOf(0));//医生收入默认0
        doctorVo.setPatientGood(BigDecimal.valueOf(100));//医生好评默认100
        doctorVo.setAskTime(0); //医生接诊次数默认0
        //默认医生头像
        doctorVo.setDoctorImg("https://1904a-lty-img.oss-cn-shanghai.aliyuncs.com/c5b83aaa-9f0e-4de3-908b-377ba02daa51.jpg");
        doctorVo.setIdCardNumber("321324199805190037");

        doctorFeignService.save(doctorVo);
        going.addDoctorRedis();
    }

}
